package com.tjetc.view.studentview;

import cn.hutool.json.JSONUtil;
import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.*;
import com.zhiyun.service.ExaminationService;
import com.zhiyun.service.TestQuestionService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.ExaminationRecordVo;
import com.zhiyun.vo.ExaminationResponseVo;
import com.zhiyun.vo.Result;

import java.text.SimpleDateFormat;
import java.util.*;

public class MyCourseView_In_Exams {
    TestQuestionService testQuestionService=CilentProxy.getProxyInstance(TestQuestionService.class);
    ExaminationService examinationService= CilentProxy.getProxyInstance(ExaminationService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(Course course){
        while (true){
            System.out.println("~~~~~~~~~~~我的考试~~~~~~~~~~");
            System.out.println("1.未开始的考试");
            System.out.println("2.正在进行的考试");
            System.out.println("3.已经考完的考试");
            System.out.println("4.我的所有考试");
            System.out.println("5.进行考试");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    noExams(course);
                    break;
                case 2:
                    examing(course);
                    break;
                case 3:
                    examed(course);
                    break;
                case 4:
                    allExams(course);
                    break;
                case 5:
                    test(course);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误,请重新输入");


            }
        }
    }

    private void test(Course course) {
        while (true){
            Result nowExaminations = examinationService.getNowExaminations(course.getId());
            if (nowExaminations.getCode()==200){
                List<Examination> obj = (List<Examination>) nowExaminations.getObj();
                System.out.println(nowExaminations.getMsg());
                for (Examination examination : obj) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    // 将Date转换为String
                    String start = sdf.format(examination.getStarttime());
                    String end = sdf.format(examination.getEndtime());
                    System.out.println("id:"+examination.getId()+"     考试名称:"+examination.getName()+"     考试简述:"+examination.getDesc()+"     开始时间:"+start+"     结束时间:"+end);
                }
                System.out.println("请选择要进行的考试id:");
                int id = scanner.nextInt();

                Examination exam=null;
                for (Examination examination : obj) {
                    if (id==examination.getId()){
                        exam=examination;
                    }
                }
                Result examinationDetails = examinationService.getExaminationDetails(id, AppData.student.getId());
                if (examinationDetails.getCode()==200){
                    ExaminationRecord obj1 = (ExaminationRecord) examinationDetails.getObj();
                    if (obj1!=null){
                        System.out.println("你已经考完试了");
                        return;
                    }
                }else {
                    System.out.println(examinationDetails.getMsg());
                }
                if (exam!=null){
                    TestPaper testPaper = exam.getTestPaper();
                    Result result = testQuestionService.selectByPaperId(testPaper.getId());
                    if (result.getCode()==200){

                        String[] strings=new String[]{"A","B","C","D","E","F"};
                        List<TestQuestion> obj1 = (List<TestQuestion>) result.getObj();
                        if (exam.getTitleOrder()==1){
                            Collections.shuffle(obj1);
                        }
                        ExaminationRecordVo examinationRecordVo = new ExaminationRecordVo(exam,new ArrayList<ExaminationResponseVo>());
                        for (TestQuestion testQuestion : obj1) {
                            if (testQuestion.getTypeId()==1){
                                String options = testQuestion.getOptions();
                                String[] strings1 = JSONUtil.parseArray(options).toArray(new String[0]);
                                if (exam.getOptionOrder()==1){
                                    strings1=ShuffleArrayExample(strings1);
                                }
                                System.out.println(testQuestion.getTitle());
                                for (int i = 0; i < strings1.length; i++) {
                                    System.out.println(strings[i]+". "+strings1[i]);
                                }
                                String answerOption=null;
                                String myAnswer = "";
                                boolean a=true;
                                while (a){
                                    System.out.println("请输入你的选择");
                                    answerOption = scanner.next();
                                    switch (answerOption) {
                                        case "A":
                                            if (strings1[0] != null){
                                                myAnswer = strings1[0];
                                                a=false;
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        case "B":
                                            if (strings1[1] != null){
                                                myAnswer = strings1[1];
                                                a=false;
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        case "C":
                                            if (strings1[2] != null){
                                                myAnswer = strings1[2];
                                                a=false;
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        case "D":
                                            if (strings1[3] != null){
                                                myAnswer = strings1[3];
                                                a=false;
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        case "E":
                                            if (strings1[4] != null){
                                                myAnswer = strings1[4];
                                                a=false;
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        case "F":
                                            if (strings1[5] != null){
                                                myAnswer = strings1[5];
                                                a=false;
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        default:
                                            System.out.println("没有该选项请重选");
                                    }

                                }
                                List<String> stringList=new ArrayList<>();
                                stringList.add(myAnswer);
                                String s = JSONUtil.toJsonStr(stringList);
                                ExaminationResponseVo examinationResponseVo = new ExaminationResponseVo(testQuestion, s);
                                examinationRecordVo.getResponseVos().add(examinationResponseVo);
                            }else if(testQuestion.getTypeId()==2){
                                String options = testQuestion.getOptions();  //获得选项
                                String[] strings1 = JSONUtil.parseArray(options).toArray(new String[0]);
                                if (exam.getOptionOrder()==1){
                                    strings1=ShuffleArrayExample(strings1);
                                }
                                System.out.println("~~~~~~~~~~多选题~~~~~~~~~~:");
                                System.out.println(testQuestion.getTitle()); //输出题目
                                for (int i = 0; i < strings1.length; i++) {
                                    System.out.println(strings[i]+". "+strings1[i]);
                                }
                                String answerOption=null;
                                String myAnswer = "";
                                boolean a=true;
                                List<String> stringList=new ArrayList<>();
                                while (a){
                                    System.out.println("请输入你的选择(输入-1进行下一题):");
                                    answerOption = scanner.next();
                                    switch (answerOption) {
                                        case "A":
                                            if (strings1[0] != null){
                                                myAnswer = strings1[0];
                                                stringList.add(myAnswer);
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        case "B":
                                            if (strings1[1] != null){
                                                myAnswer = strings1[1];
                                                stringList.add(myAnswer);
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        case "C":
                                            if (strings1[2] != null){
                                                myAnswer = strings1[2];
                                                stringList.add(myAnswer);
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        case "D":
                                            if (strings1[3] != null){
                                                myAnswer = strings1[3];
                                                stringList.add(myAnswer);
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        case "E":
                                            if (strings1[4] != null){
                                                myAnswer = strings1[4];
                                                stringList.add(myAnswer);
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        case "F":
                                            if (strings1[5] != null){
                                                myAnswer = strings1[5];
                                                stringList.add(myAnswer);
                                            }else {
                                                System.out.println("没有该选项请重选");
                                            }
                                            break;
                                        case "-1":
                                            a=false;
                                            break;
                                        default:
                                            System.out.println("没有该选项请重选");
                                    }

                                }


                                String s = JSONUtil.toJsonStr(stringList);
                                ExaminationResponseVo examinationResponseVo = new ExaminationResponseVo(testQuestion, s);
                                examinationRecordVo.getResponseVos().add(examinationResponseVo);
                            }else if(testQuestion.getTypeId()==3){
                                System.out.println("~~~~填空题~~~");
                                System.out.println(testQuestion.getTitle()); //输出题目
                                System.out.println("请输入你的答案");
                                String next = scanner.next();
                                List<String> stringList=new ArrayList<>();
                                stringList.add(next);
                                String s = JSONUtil.toJsonStr(stringList);
                                ExaminationResponseVo examinationResponseVo = new ExaminationResponseVo(testQuestion, s);
                                examinationRecordVo.getResponseVos().add(examinationResponseVo);
                            }else if(testQuestion.getTypeId()==4){
                                System.out.println("~~~~简答题~~~");
                                System.out.println(testQuestion.getTitle()); //输出题目
                                System.out.println("请输入你的答案");
                                String next = scanner.next();
                                List<String> stringList=new ArrayList<>();
                                stringList.add(next);
                                String s = JSONUtil.toJsonStr(stringList);
                                ExaminationResponseVo examinationResponseVo = new ExaminationResponseVo(testQuestion, s);
                                examinationRecordVo.getResponseVos().add(examinationResponseVo);
                            }


                        }
                        Result submit = examinationService.submit(examinationRecordVo, AppData.student.getId());
                        System.out.println(submit.getMsg());


                    }else {
                        System.out.println(result.getMsg());
                    }
                }
            }else {
                System.out.println(nowExaminations.getMsg());
            }
            break;
        }
    }

    private void noExams(Course course) {
        while (true){
            Result notStartExaminations = examinationService.getNotStartExaminations(course.getId());
            if (notStartExaminations.getCode()==200){
                List<Examination> obj = (List<Examination>) notStartExaminations.getObj();
                System.out.println(notStartExaminations.getMsg());
                for (Examination examination : obj) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    // 将Date转换为String
                    String start = sdf.format(examination.getStarttime());
                    String end = sdf.format(examination.getEndtime());
                    System.out.println("id:"+examination.getId()+"     考试名称:"+examination.getName()+"     考试简述:"+examination.getDesc()+"     开始时间:"+start+"     结束时间:"+end);
                }
            }else {
                System.out.println(notStartExaminations.getMsg());
            }
            break;
        }
    }

    private void examing(Course course) {
        while (true){
            Result nowExaminations = examinationService.getNowExaminations(course.getId());
            if (nowExaminations.getCode()==200){
                List<Examination> obj = (List<Examination>) nowExaminations.getObj();
                System.out.println(nowExaminations.getMsg());
                for (Examination examination : obj) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    // 将Date转换为String
                    String start = sdf.format(examination.getStarttime());
                    String end = sdf.format(examination.getEndtime());
                    System.out.println("id:"+examination.getId()+"     考试名称:"+examination.getName()+"     考试简述:"+examination.getDesc()+"     开始时间:"+start+"     结束时间:"+end);
                }
            }else {
                System.out.println(nowExaminations.getMsg());
            }
            break;
        }
    }

    private void examed(Course course) {
        while (true){

            Result endExaminations = examinationService.getEndExaminations(course.getId());
            if (endExaminations.getCode()==200){
                List<Examination> obj = (List<Examination>) endExaminations.getObj();
                System.out.println(endExaminations.getMsg());
                for (Examination examination : obj) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    // 将Date转换为String
                    String start = sdf.format(examination.getStarttime());
                    String end = sdf.format(examination.getEndtime());
                    System.out.println("id:"+examination.getId()+"     考试名称:"+examination.getName()+"     考试简述:"+examination.getDesc()+"     开始时间:"+start+"     结束时间:"+end);
                }
                System.out.println("是否查看某次考试详情(Y/N)");
                String next = scanner.next();
                if (next.equals("Y")){
                    System.out.println("请输入想要查看的考试id");
                    int i = scanner.nextInt();
                    Examination examination1=null;
                    for (Examination examination : obj) {
                        if (i==examination.getId()){
                            examination1=examination;
                        }
                    }
                    if (examination1!=null){
                        Result examinationDetails = examinationService.getExaminationDetails(examination1.getId(), AppData.student.getId());
                        if (examinationDetails.getCode()==200){
                            ExaminationRecord obj1 = (ExaminationRecord) examinationDetails.getObj();

                            if (obj1!=null){
                                List<Response> responses = obj1.getResponses();
                                System.out.println("考试成绩: "+obj1.getScore());
                                for (Response respons : responses) {
                                    TestQuestion testQuestion = respons.getTestQuestion();
                                    Integer typeId = testQuestion.getTypeId();
                                    if (typeId==1||typeId==2){
                                        System.out.println("题目:   "+testQuestion.getTitle());
                                        String[] strings = JSONUtil.parseArray(testQuestion.getOptions()).toArray(new String[0]);
                                        for (String string : strings) {
                                            System.out.println(string);
                                        }

                                        System.out.println("正确答案:  ");
                                        String[] strings2 = JSONUtil.parseArray(testQuestion.getAnswer()).toArray(new String[0]);
                                        for (String s : strings2) {
                                            System.out.println(s);
                                        }


                                        String[] strings1 = JSONUtil.parseArray(respons.getContent()).toArray(new String[0]);
                                        System.out.println("学生答案");
                                        for (String s : strings1) {
                                            System.out.println(s);
                                        }
                                        System.out.println("我的得分   "+respons.getScore());
                                    }
                                    if (typeId==3||typeId==4){
                                        System.out.println("题目:   "+testQuestion.getTitle());
                                        System.out.println("正确答案:  "+ JSONUtil.toJsonStr(testQuestion.getAnswer()));
                                        System.out.println("学生答案:  "+ JSONUtil.toJsonStr(respons.getContent()));
                                        System.out.println("我的得分   "+respons.getScore());
                                    }
                                }
                            }else {
                                System.out.println("此次考试未及时参加~~");
                            }



                        }


                    }



                }else if(next.equals("N")){
                    break;
                }else {
                    System.out.println("输入错误请重新输入");
                }
            }else {
                System.out.println(endExaminations.getMsg());
            }
            break;
        }
    }

    private void allExams(Course course) {
        while (true){
            Result examinations = examinationService.getExaminations(course.getId());
            if (examinations.getCode()==200){
                List<Examination> obj = (List<Examination>) examinations.getObj();
                System.out.println(examinations.getMsg());
                for (Examination examination : obj) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    // 将Date转换为String
                    String start = sdf.format(examination.getStarttime());
                    String end = sdf.format(examination.getEndtime());
                    System.out.println("id:"+examination.getId()+"     考试名称:"+examination.getName()+"     考试简述:"+examination.getDesc()+"     开始时间:"+start+"     结束时间:"+end);
                }
            }else {
                System.out.println(examinations.getMsg());
            }
            break;
        }
    }
    public static String[] ShuffleArrayExample(String[] string){
        // 将数组转换为 List
        List<String> list = Arrays.asList(string);

        // 使用 Collections.shuffle 乱序
        Collections.shuffle(list);

        // 转换回数组（如果需要）
        String[] shuffledArray = list.toArray(new String[0]);
        return shuffledArray;
    }
}
